package common;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;

// TODO substitute

@SuppressWarnings("serial")
public class Configuration implements Serializable {

	private static final String HOME = "appserver.home";
	private static final String PATH = "etc";

	/**
	 * The loaded properties are held here.
	 */
	protected Properties properties;

	/**
	 * The system property key that refers to the file name of the properties to
	 * be loaded.
	 */
	protected String systemPropertyKey;

	/**
	 * The default file location; just in case, the system property for the
	 * location hasn't been set.
	 */
	protected String defaultFileName;

	public static String formDefaultConfigurationPath(String filename) {
		return formDefaultPath(PATH, filename);
	}

	public static String formDefaultPath(String path, String filename) {
		StringBuffer location = new StringBuffer();
		location.append(System.getProperty(HOME, System.getProperty("user.dir")));
		location.append(File.separator);
		location.append(path);
		location.append(File.separator);
		location.append(filename);
		return location.toString();
	}

	public Configuration(String systemPropertyKey, String defaultFileName) {
		this.systemPropertyKey = systemPropertyKey;
		this.defaultFileName = defaultFileName;
		properties = new Properties();

		loadProperties();
	}

	/**
	 * Getter for property properties.
	 * 
	 * @return Value of property properties.
	 */
	public Properties getProperties() {
		return properties;
	}

	/**
	 * Load the properties from the file location specified by the
	 * systemPropertyKey or by the defaultFileLocation if the systemPropertyKey
	 * is null.
	 */
	public void loadProperties() {
		String propFileName;
		if (systemPropertyKey != null) {
			propFileName = System.getProperty(systemPropertyKey, formDefaultConfigurationPath(defaultFileName));
		} else {
			propFileName = formDefaultConfigurationPath(defaultFileName);
		}

		File file = new File(propFileName);
		if (file.exists()) {
			FileInputStream fis = null;
			try {
				fis = new FileInputStream(propFileName);
				Properties loadedProperties = new Properties();
				loadedProperties.load(fis);
				properties = loadedProperties;

			} catch (IOException e) {
				LogCategory.getCategory(this.getClass().getName()).debug("Properties Not Found", e);
			} finally {
				try {
					if (fis != null) {
						fis.close();
					}
				} catch (Exception e) {
				}
			}
		} else {
			Class<?> c = this.getClass();
			InputStream is = c.getResourceAsStream(PATH + defaultFileName);
			if (is == null) {
				LogCategory.getCategory(this.getClass().getName()).debug("Properties Not Found");
				return;
			}
			Properties loadedProperties = new Properties();
			try {
				loadedProperties.load(is);
				properties = loadedProperties;
			} catch (IOException e) {
				LogCategory.getCategory(this.getClass().getName()).debug("Properties Not Found", e);
			} finally {
				try {
					if (is != null) {
						is.close();
					}
				} catch (Exception e) {
				}
			}
		}
	}
}
